package com.contract.management.util;

import java.util.Map;

import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;
import org.thymeleaf.templateresolver.ClassLoaderTemplateResolver;

/**
 * 采用thymeleaf库解析html模板。
 * 该模板必须放在resources/static/templdate目录下, 扩展名为.html
 * 
 * @author zhangxl
 *
 */
public class TmplUtils {
	
	private static class HtmlEngineHolder {
		private static final ClassLoaderTemplateResolver INSTANCE = new ClassLoaderTemplateResolver();
		private static final TemplateEngine ENGINE = new TemplateEngine();
		static {
			INSTANCE.setPrefix("static/template/");
			INSTANCE.setSuffix(".html");
			INSTANCE.setCharacterEncoding("UTF-8");
			ENGINE.setTemplateResolver(INSTANCE);
		}
	}
	
	private static final TemplateEngine getHtmlEngine() {
		return HtmlEngineHolder.ENGINE;
	}
	
	/**
	 * 解析html模板。
	 * 
	 * @param filename	文件名，不包括.html后缀
	 * @param data		填充数据
	 * @return
	 */
	public static String  parseHtml(String filename, Map<String, Object> data) {
		Context context = new Context();
		context.setVariables(data);
        return getHtmlEngine().process(filename, context);
	}
}
